$(function () {
    let markId = getUrlString('markid');
    let page = -1,totalSize = 0;
    let page1 = -1,totalSize1 = 0,key;
    let mRefresh;

    //搜索用户
    function userlist(page,nick) {
        var data = {
            'page':page,
            'nick':nick
        };
        getData('post', '/niu/apiv3/member_getuserlist.php', data, function (data) {
            console.log(data);
            if(data.pagetotal === 0){
                $('#mrefresh').hide();
                $('.nullContent').show();
            }else {
                $('.nullContent').hide();
                let append = '';
                $(data.data).each(function () {
                    append += ' <li>' +
                        '                    <img src="'+this.faceurl+'" alt="">' +
                        '                    <span>'+this.nick+'</span>' +
                        '                    <span class="addFrd">申请好友</span>' +
                        '<input type="hidden" class="id" value="'+this.id+'">'+
                        '                </li>'
                });
                $('#searchList').append(append);
                $('#searchList li .addFrd').click(function () {
                    let id = $(this).next('.id').val();
                    window.location.href = 'dancool://fullweb=https://api.odancool.com/weixinapp/view/messages/verifyFrd.html?id='+id;
                });
                totalSize1 = totalSize1 + 10;
                if (totalSize1 > data.datacount || totalSize1 === data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }
        })
    }

    let msgMRefresh = new MiniRefresh({
        container: '#msgMrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#conversationList').empty();
                totalSize = 0;
                page = -1;
                msgMRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                getFriendInviteList(page)
            }
        }
    });



    //搜索
    $('#searchBtn').click(function () {
        $('#msgMrefresh').hide();
        key = $('#searchVal').val();
        $('#searchList').empty();
        if(key === '' || key === undefined){
            laymsg('搜索不能为空');
        }else {
            $('#mrefresh').show();
            mRefresh = new MiniRefresh({
                container: '#mrefresh', //这里是插件的容器ID
                down: {
                    isAuto: false,
                    callback: function () {
                        // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                        $('#searchList').empty();
                        totalSize1 = 0;
                        page1 = -1;
                        mRefresh.endDownLoading();
                    }
                }
                ,
                up: {
                    isAuto: true,
                    callback: function () {
                        // 向上拉后的回调事件
                        page1 += 1;
                        userlist(page1,key)
                    }
                }
            });
        }
    });

    //验证消息
    function getFriendInviteList (page){
        let data = {
            'markid':markId,
            'page':page,
            'status':'1'
        };
        getData('get','/niu/apiv3/weibo_friend.php?method=getFriendInviteList',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                let append = '';
                if (data.datacount === 0){
                    $('#msgMrefresh').hide();
                    return
                }else {
                    $('#msgMrefresh').show();
                }
                $(data.data).each(function () {
                    append += '<li>' +
                        '                    <div class="left">' +
                        '                        <img src="'+this.faceurl+'" alt="">' +
                        '                    </div>' +
                        '                    <div class="right">' +
                        '                        <p class="username">' +
                        '                            <span>'+this.nick+'</span>' +
                        '                        </p>' +
                        '                        <p class="userContent">' +
                        '                            <span class="messageContent">'+this.reason1+'</span>' +
                        '                        </p>' +
                        '                    </div>' +
                        '                    <div class="consent">同意</div>' +
                        '                    <input class="id" type="hidden" value="'+this.id+'">'+
                        '                    <div class="turn">拒绝</div>' +
                        '                    <div class="isConsent">已同意</div>'+
                        '                </li>'
                });
                $('#conversationList').append(append);

                $('#conversationList li .consent').click(function () {
                    let id = $(this).next('.id').val();
                    let _this = $(this);
                    getData('get','/niu/apiv3/weibo_friend.php?method=acceptInvitation',{'inviteid':id,'markid':markId},function (data) {
                        console.log(data);
                        if (data.resultcode === '0'){
                            _this.nextAll('.turn').hide();
                            _this.hide();
                            _this.nextAll('.isConsent').show();
                        }
                    })
                });

                $('#conversationList li .turn').click(function () {
                    let id = $(this).prev('.id').val();
                    let _this = $(this);
                    getData('get','/niu/apiv3/weibo_friend.php?method=declineInvitation',{'inviteid':id,'markid':markId,'info':'拒绝'},function (data) {
                        console.log(data);
                        if (data.resultcode === '0'){
                            _this.prevAll('.consent').hide();
                            _this.hide();
                            _this.nextAll('.isConsent').html('已拒绝').show();
                        }
                    })
                });

                totalSize = totalSize + 10;
                if (totalSize > data.datacount)
                    msgMRefresh.endUpLoading(true);
                else{
                    msgMRefresh.endUpLoading(false);
                }
            }
        })
    }

    function getnative(actionurl) {
        let u = navigator.userAgent;
        let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            window.webkit.messageHandlers.getnative.postMessage(actionurl);
        }else {
            ikags.getnative(actionurl)
        }
    }

    function jsnativeresult(tag1,tag2,tag3) {
        alert(tag1);
    }
});